package com.jtjrenren.android.taxi.driver.core.wallect;

import android.content.Context;
import android.content.Intent;
import android.text.InputType;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;


import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.bean.api.ApiBindPayResult;
import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.core.other.selectCityDActivity;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.driver.R;

import com.library.weiget.ClearEditText;
import com.library.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/5/6.
 */
public class WallectBindPayActivity extends BaseActivity {
    @BindView(R.id.user_bindpay_name)
    EditText et_name;
    @BindView(R.id.user_bindpay_account)
    ClearEditText et_account;
    @BindView(R.id.user_bindpay_accountdesc)
    TextView tv_accountdesc;
    @BindView(R.id.user_bindpay_bind)
    Button bt_bind;
    @BindView(R.id.user_bindpay_xzyhcon)
    LinearLayout ll_yhlistcon;
    @BindView(R.id.user_bindpay_yhlist)
    Spinner yh_list;
    @BindView(R.id.user_bindpay_ss)
    TextView tv_ss;

    private String pro = "", city = "";


    public int currentType = Constants.TYPE_PAY_YHK;

    private int currentMode = 0; //0代表没有绑定，1代表已经绑定还没有编辑 ，2代表编辑模式
    private DriverInfo driver;

    public static Intent getDiyIntent(Context context,int type){
        Intent intent=new Intent(context,WallectBindPayActivity.class);
        intent.putExtra("type",type);
        return intent;
    }

    @Override
    public int getLayoutID() {
        return R.layout.aty_wallect_bindpay;
    }

    @Override
    public void init() {
        if (getIntent().hasExtra("type")) {
            currentType = getIntent().getIntExtra("type", Constants.TYPE_PAY_YHK);
        }

        driver = AppManger.getInstance().driver;


        ((TextView) findViewById(R.id.title_title)).setText("支付方式绑定");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        if (currentType == Constants.TYPE_PAY_YHK) {
            ll_yhlistcon.setVisibility(View.VISIBLE);
            tv_accountdesc.setText(getString(R.string.yinhangkahao));
            et_account.setInputType(InputType.TYPE_CLASS_NUMBER);
        } else {
            ll_yhlistcon.setVisibility(View.GONE);
            tv_accountdesc.setText(getString(R.string.zhifubaozhanghao));
            et_account.setInputType(InputType.TYPE_CLASS_TEXT);
        }

        if (driver != null)
            et_name.setText(driver.getName());
        et_name.setEnabled(false);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.bank_type,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        yh_list.setAdapter(adapter);

        refreshBindMsg();
    }


    @OnClick(R.id.user_bindpay_bind)
    public void clickBindPay(){
        if (currentMode == 0) {
            doBind();
        } else if (currentMode == 1) {
            currentMode = 2;
            refreshView();
        } else if (currentMode == 2) {
            doBind();
        }
    }

    @OnClick(R.id.user_bindpay_ss)
    public void clickSs(){
        Intent intent = new Intent(this, selectCityDActivity.class);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                pro = data.getStringExtra("p");
                city = data.getStringExtra("c");
                String desc = pro + city;
                tv_ss.setText(desc);
            }
        }
    }

    private void doBind() {
        String name = et_name.getText().toString().trim();
        String account = et_account.getText().toString().trim();
        String bankCode = "";
        if (currentType == Constants.TYPE_PAY_YHK)
            bankCode = yh_list.getSelectedItem().toString().trim();

        if (StringUtils.isEmpty(account)) {
            displayShort(getString(R.string.qingshuruzhengquedezhanghao));
            return;
        }

        if (driver == null)
            return;
        showProDialog(null, getString(R.string.bangdingzhognqingshaohou));

        ApiWorks.SetBankBindingData_M(driver.getWorkNo(), name, account, bankCode, currentType, pro, city, new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                disProDialog();
                if (commonResult != null && commonResult.getCode() == 1) {
                    displayShort(getString(R.string.bangdingchenggong));
                    setResult(RESULT_OK);
                    finish();
                } else
                    displayShort(getString(R.string.bangdingshibai));
            }
        });
    }

    private void refrshInitView(ApiBindPayResult bindPayResult) {
        if (bindPayResult != null && !StringUtils.isEmpty(bindPayResult.getAccount())) {
            currentMode = 1;
            refreshView();

            et_account.setText(bindPayResult.getAccount());
            pro = bindPayResult.getProvince();
            city = bindPayResult.getCity();
            tv_ss.setText(bindPayResult.getProvince() + bindPayResult.getCity());

            if (currentType == Constants.TYPE_PAY_YHK) {
                String[] bankTypes = getResources()
                        .getStringArray(R.array.bank_type);
                int position = 0;
                for (int i = 0; i < bankTypes.length; i++) {
                    if (bindPayResult.getBankCode().equals(bankTypes[i])) {
                        position = i;
                        break;
                    }
                }
                yh_list.setSelection(position);
            }

        } else {
            currentMode = 0;
            refreshView();
        }
    }

    private void refreshView() {
        if (currentMode == 0) {
            et_account.setEnabled(true);
            yh_list.setEnabled(true);
            tv_ss.setEnabled(true);
            bt_bind.setText(getString(R.string.bangding));
        } else if (currentMode == 1) {
            et_account.setEnabled(false);
            yh_list.setEnabled(false);
            tv_ss.setEnabled(false);
            bt_bind.setText(getString(R.string.xiugai));
        } else if (currentMode == 2) {
            et_account.setEnabled(true);
            yh_list.setEnabled(true);
            tv_ss.setEnabled(true);
            bt_bind.setText(getString(R.string.baocun));
        }
    }

    private void refreshBindMsg() {
        if (driver == null)
            return;
        ApiWorks.getPayBindingData(driver.getWorkNo(), currentType, new ApiWorks.ResponseListener<ApiBindPayResult>() {
            @Override
            public void onResponse(ApiBindPayResult apiBindPayResult) {
                if (apiBindPayResult != null)
                    refrshInitView(apiBindPayResult);
            }
        });
    }
}
